package com.atguigu.spzx.manage.controller;

import com.atguigu.spzx.manage.service.SysUserService;
import com.atguigu.spzx.manage.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "系统登录接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {


    @Autowired
    private SysUserService sysUserService ;
    @Autowired
    private ValidateCodeService validateCodeService;



    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        //用户名userName,密码password,提交验证码captcha,验证码codeKey
        log.info("登录用户为：{}",loginDto);
        //令牌,刷新令牌(可为空)
        LoginVo loginVo = sysUserService.login(loginDto) ;
        return Result.build(loginVo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "生成验证码接口")
    @GetMapping(value = "/generateValidateCode")
    //验证码codeKey,图片验证码对应的字符串数据codeValue
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }


    @Operation(summary = "获取用户信息")
   @GetMapping(value = "/getUserInfo")
   public Result<SysUser> getUserInfo() {

        return Result.build(AuthContextUtil.get()  , ResultCodeEnum.SUCCESS) ;
   }


    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

}
